package com.ddstation.crm.outpatient.service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ddstation.common.property.AgeProperties;
import com.ddstation.common.util.AgeUtil;
import com.ddstation.crm.outpatient.entity.DtHealthDataOutpatientMedicalEntity;
import com.ddstation.crm.outpatient.mapper.DtHealthDataOutpatientMedicalMapper;
import com.ddstation.crm.outpatient.mapper.DtHealthDataWestPrescriptionMapper;
import com.ddstation.crm.outpatient.model.OutpatientModel;
import com.ddstation.crm.outpatient.model.WestPrescriptionModel;
import com.ddstation.patient.model.UserModel;
import com.ddstation.patient.service.UserInfoServiceItf;
import com.ddstation.user.health_file.entity.DtHealthDataAllergiesUserEntity;
import com.ddstation.user.health_file.mapper.DtHealthDataAllergiesUserMapper;
import com.ddstation.user.health_file.model.sub.HealthdataUserModel;

@Service @Lazy
public class OutpatientServiceImpl implements OutpatientService {
	
	private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	@Transactional(readOnly=true)
	public OutpatientModel load(String uid, int childId, String medicalId, Integer type) {
		
		OutpatientModel outpatientModel = new OutpatientModel();
		
		// 处方头部打印信息
		outpatientModel.setPrintInfo(this.getPrintInfo(uid, childId, medicalId));
		
		// 病人信息查询
		HealthdataUserModel healthdatauser = new HealthdataUserModel();
		
		// 门诊信息查询
		outpatientModel.setMedical(this.loadMedical(medicalId));
		
		// 西药处方信息查询
		WestPrescriptionModel westPrescriptionModel = new WestPrescriptionModel();
		outpatientModel.setWestPrescription(westPrescriptionModel);
		if (type == 2) {
			if(medicalId.matches("[0-9]+")) {
				westPrescriptionModel.setPrescriptions(dtHealthDataWestPrescriptionMapper.selectByParans(uid, childId, medicalId));
			}else{
				westPrescriptionModel.setPrescriptions(dtHealthDataWestPrescriptionMapper.selectByParansForOutHis(uid, childId, medicalId));
			}
//					westPrescriptionModel.setExtendList(dtHealthDataWestPrescriptionExtendMapper.select(medicalId));
		}
		
		//　门诊病历：中成药处方
//				ChinesepatentPrescriptionModel chinesepatentPrescriptionModel = new ChinesepatentPrescriptionModel();
//				outpatientModel.setChinesepatentPrescription(chinesepatentPrescriptionModel);
//				if (type == 4) {
//					chinesepatentPrescriptionModel.setPrescriptions(dtHealthDataChinesepatentPrescriptionMapper.select(uid, childId, medicalId));
//					chinesepatentPrescriptionModel.setExtendList(dtHealthDataChinesePatentPrescriptionExtendMapper.select(medicalId));
//				}
		
		UserModel user = userInfoService.fetchBaseUserInfo(uid, childId);
		if (user != null) {
			healthdatauser.setName(user.getName());
			healthdatauser.setGender(user.getGender());
			healthdatauser.setAge(user.getAge());
			healthdatauser.setFimalyAddr(user.getFimalyAddr());
			healthdatauser.setFimalyAddrAllTxt(user.getFimalyAddrAllTxt());
			healthdatauser.setPhone(user.getPhone());
		} else {
			throw new RuntimeException("用户数据不存在 uid" + uid);
		}
		outpatientModel.setUser(healthdatauser);
		
		return outpatientModel;
}
	
	@Override
	@Transactional(readOnly=true)
	public DtHealthDataOutpatientMedicalEntity loadMedical(String medicalId) {
		// TODO Auto-generated method stub
		// 检索门诊病历：病历
		DtHealthDataOutpatientMedicalEntity medical = dtHealthDataOutpatientMedicalMapper.selectByPK(medicalId);
		if (medical != null) {
			medical.setShowAge(AgeUtil.getAge4View(medical.getAge(), medical.getAgeMonth(), AgeProperties.LIMIT));
			if (medical.getVisitDate() != null) {
				// 门诊日期格式化
				medical.setVisitDateTxt(sdf.format(medical.getVisitDate()));
			}
		}
		return medical;
	}
	
	@Override
	@Transactional(readOnly=true)
	public Map<String, Object> getPrintInfo( String uid,int childId, String medicalID) {
		//门诊
		DtHealthDataOutpatientMedicalEntity medical = dtHealthDataOutpatientMedicalMapper.selectByPK(medicalID);
		//　过敏史
		List<DtHealthDataAllergiesUserEntity> dataList = dtHealthDataAllergiesUserMapper.select(uid, childId);
		
		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("diagnosis", medical.getDiagnosis()==null?"-":medical.getDiagnosis());
		StringBuilder allergies = new StringBuilder();
		for(DtHealthDataAllergiesUserEntity data:dataList) {
			if (allergies.length() > 0) {
				allergies.append("、");
			}
			allergies.append(data.getItemName());
		}
		retMap.put("allergies", allergies.length()==0?"-":allergies.toString());
		
		return retMap;
	}
	
	@Inject private DtHealthDataOutpatientMedicalMapper dtHealthDataOutpatientMedicalMapper;
	@Inject private DtHealthDataWestPrescriptionMapper dtHealthDataWestPrescriptionMapper;
	@Inject private DtHealthDataAllergiesUserMapper<DtHealthDataAllergiesUserEntity> dtHealthDataAllergiesUserMapper;
	@Inject private UserInfoServiceItf userInfoService;
}